/*
* module: @/views/budget/list_download/searchSQL
* author:zengfx
* data:2020-11-30
*/
<template>
  <div class="searchSQL_container">
    <header>
      <p>本地清单来源配置</p>
      <!-- <el-button size="mini" @click="handle_back">返回</el-button> -->
    </header>
    <main>
      <el-button size="mini" type="primary" @click="handle_add" :disabled="disabledBtn">添加</el-button>
      <div class="table" :style="{height:height + 'px'}">
        <ul>
          <li v-for="(item,index) in sqlList" :key="index">
            <p>
              <label>报表名称：</label>
              <el-input type="text" placeholder="请输入报表名称" v-model="item.rportnanme"></el-input>
            </p>
            <p>
              <label>SQL编号：</label>
              <el-input type="text" placeholder="请输入SQL编号" v-model="item.numberD"></el-input>
            </p>
            <p class="flexStart">
              <label>SQL语句：</label>
              <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" placeholder="请输入SQL语句" v-model="item.sqlcontent"></el-input>
            </p>
            <p>
              <el-button size="mini" type="primary" :disabled="!(item.rportnanme&&item.numberD&&item.sqlcontent)" @click="handle_click(item)">{{item.btnText}}</el-button>
              <el-button size="mini" type="danger" @click="handle_deleteSQL(item)">删除</el-button>
            </p>
          </li>
        </ul>
      </div>
    </main>
  </div>
</template>

<script>
import { $budgetAPI } from '@/api';
export default {
    data() {
        return {
            height: '',
            sqlList: [],
            tableLoading: false,
            disabledBtn: false
        };
    },
    methods: {
        getHeight() {
            this.height = window.innerHeight - 83 - 54 - 51 - 40 - 10;
        },
        getInitData() {
            this.tableLoading = true;
            $budgetAPI.getReportSQLList(
                {},
                res => {
                    if (res.code == 0) {
                        res.content.data.map(item => {
                            item.btnText = '修改';
                        });
                        this.sqlList = res.content.data;
                    }

                    this.tableLoading = false;
                },
                err => {
                    this.tableLoading = false;
                }
            );
        },
        handle_click(item) {
            item.name == '修改' ? this.handle_edit(item) : this.handle_save(item);
        },
        // 新增
        handle_add() {
            let obj = {
                numberD: '',
                rportnanme: '',
                sqlcontent: '',
                btnText: '保存'
            };
            this.sqlList.unshift(obj);
            this.disabledBtn = true;
        },
        // 修改
        handle_edit(item) {
            let params = {
                numberD: item.numberD,
                id: item.id,
                rportnanme: item.rportnanme,
                sqlcontent: item.sqlcontent
            };
            this.tableLoading = true;
            $budgetAPI.modifySQL(params, res => {
                if (res.code == 0) {
                    this.$message({
                        message: '修改成功',
                        type: 'success'
                    });
                    this.getInitData();
                } else {
                    this.$message.error(res.message);
                }
            });
        },
        // 保存
        handle_save(item) {
            let params = {
                numberD: item.numberD,
                rportnanme: item.rportnanme,
                sqlcontent: item.sqlcontent
            };
            this.tableLoading = true;
            $budgetAPI.insertSQL(params, res => {
                if (res.code == 0) {
                    this.$message({
                        message: '保存成功',
                        type: 'success'
                    });
                    this.disabledBtn = false;
                    this.getInitData();
                } else {
                    this.$message.error(res.message);
                }
            });
        },
        // 删除sql
        handle_deleteSQL(item) {
            let params = {
                numberD: item.numberD,
                id: item.id,
                rportnanme: item.rportnanme,
                sqlcontent: item.sqlcontent
            };
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    if (item.btnText == '保存' || item.numberD == '') {
                        this.sqlList.splice(0, 1);
                        this.disabledBtn = false;
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        return;
                    }
                    $budgetAPI.deleteSQL(params, res => {
                        if (res.code == 0) {
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getInitData();
                        } else {
                            this.$message.error(res.message);
                        }
                    });
                })
                .catch(() => {});
        }
    },
    mounted() {
        this.getHeight();
        this.getInitData();
    }
};
</script>

<style lang="scss" scoped>
.searchSQL_container {
    & > header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        border-bottom: 1px solid #eaeaea;
        height: 40px;
        margin: 0 -20px;
        padding: 0 20px;
        & > p {
            font-weight: bold;
            font-size: 15px;
        }
    }
    & > main {
        margin-top: 10px;
        padding: 10px;
        .table {
            overflow-y: auto;
            & > ul {
                list-style: none;
                padding: 20px;
                & > li {
                    display: flex;
                    align-items: flex-start;
                    flex-direction: row;
                    margin-bottom: 20px;
                    border-bottom: 1px solid #efede8;
                    padding-bottom: 20px;
                    &:last-child {
                        border-bottom: none;
                    }
                    & > p {
                        width: 30%;
                        display: flex;
                        align-items: center;
                        margin-right: 20px;
                        &.flexStart {
                            align-items: flex-start;
                            & > label {
                                padding-top: 10px;
                            }
                        }
                        &:last-child {
                            width: 10%;
                            flex-direction: column;
                            margin-right: 0;
                            & > button:first-child {
                                margin-bottom: 10px;
                            }
                            & > button:last-child {
                                margin-left: 0;
                            }
                        }
                        & > label {
                            display: block;
                            width: 80px;
                            text-align: right;
                        }
                    }
                }
            }
        }
    }
}
</style>